package com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.css.fxfzxczh.base.attachment.service.AttachmentInfoService;
import com.css.fxfzxczh.common.utils.UpdateUtil;
import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.entity.EmergencyReport;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.param.EmergencyReportParam;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.repository.EmergencyReportRepository;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.service.EmergencyReportService;
import com.css.fxfzxczh.modules.disasterSituationDataManage.emergencyReport.vo.EmergencyReportVO;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.service.EarEveManageService;
import com.css.fxfzxczh.util.EasyPoiExcelUtil;
import com.css.fxfzxczh.util.PlatformDateUtils;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.web.PlatformSessionContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/01/30
 * @Description: 灾情急报Service
 */
@Service
public class EmergencyReportServiceImpl implements EmergencyReportService {

    @Resource
    EmergencyReportRepository emergencyReportRepository;
    @Resource
    AttachmentInfoService attachmentInfoService;
    @Resource
    EarEveManageService earEveManageService;

    @Override
    public Map<String, Object> getPage(EmergencyReportParam queParams, int curPage, int pageSize) {
        return emergencyReportRepository.getPage(queParams, curPage, pageSize);
    }

    @Override
    public void exportData(HttpServletResponse response, EmergencyReportParam param) {
        List<EmergencyReport> list = emergencyReportRepository.queryByParam(param);
        EasyPoiExcelUtil.exportExcel(list, "灾情急报", "sheet1", EmergencyReport.class, "灾情急报", response);
    }

    @Override
    public EmergencyReportVO findById(String id) {
        EmergencyReport emergencyReport = emergencyReportRepository.findById(id);
        EmergencyReportVO emergencyReportVO = new EmergencyReportVO();
        UpdateUtil.copyNullProperties(emergencyReport,emergencyReportVO);
        if (PlatformObjectUtils.isNotEmpty(emergencyReport.getEnclosurePhoto())){
            emergencyReportVO.setEnclosurePhoto(attachmentInfoService.queryFile(emergencyReport.getEnclosurePhoto()));
        }
        if (PlatformObjectUtils.isNotEmpty(emergencyReport.getEnclosureVideo())){
            emergencyReportVO.setEnclosureVideo(attachmentInfoService.queryFile(emergencyReport.getEnclosureVideo()));
        }
        if (PlatformObjectUtils.isNotEmpty(emergencyReport.getEnclosureAudio())){
            emergencyReportVO.setEnclosureAudio(attachmentInfoService.queryFile(emergencyReport.getEnclosureAudio()));
        }
        if (PlatformObjectUtils.isNotEmpty(emergencyReport.getEnclosureAlbum())){
            emergencyReportVO.setEnclosureAlbum(attachmentInfoService.queryFile(emergencyReport.getEnclosureAlbum()));
        }
        return emergencyReportVO;
    }

    //新增灾情急报
    @Override
    public void save(EmergencyReport param) {
        param.setXderId(UUID.randomUUID().toString().replaceAll("-", ""));
        param.setDelFlag(YNEnum.N.toString());
        param.setCreateTime(new Date());
        param.setSubmit(YNEnum.N.toString());
        param.setGeom("POINT("+param.getLongitude()+" " +param.getLatitude()+")");
        emergencyReportRepository.save(param);
    }

    //提交
    @Override
    public void submit(String id) {
        emergencyReportRepository.submit(id);
    }

    @Override
    public void update(EmergencyReport param) {
        //param.setUpdateUser(PlatformSessionContext.getUserID());
        param.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        emergencyReportRepository.update(param);
    }

    @Override
    public void batchDelete(List<String> ids) {
        emergencyReportRepository.batchDelete(ids);
    }
    @Override
    public String getCode(String eventCode) {
        EarEveManage currentEvent = earEveManageService.getCurrentEvent();
        eventCode=currentEvent.getEventCode();
        StringBuilder code = new StringBuilder(eventCode+"ZQJB");
        DecimalFormat decimalFormat=new DecimalFormat("000");
        String num="";

        String maxNum=emergencyReportRepository.getCode(code.toString());
        if(PlatformObjectUtils.isEmpty(maxNum)){
            int i = 1;
            num=decimalFormat.format(i);
        }else{
            String substring = maxNum.substring(maxNum.length()-3);
            num = decimalFormat.format(Integer.parseInt(substring) + 1);
        }
        return code.append(num).toString();
    }
}
